﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="user.aspx.cs" Inherits="user" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ import Namespace="RobozzleModel" %>
<%@ Import Namespace="System.Linq" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="stats.css" rel="stylesheet" type="text/css"/>
    <title>RoboZZle stats for <% Response.Write(Server.HtmlEncode(Request.Params["name"])); %></title>
</head>
<body>
<%= StatsCommon.MenuBar(MenuPage.None)%>
     <%
        string userName = Request.Params["name"];
        if (userName != null) userName = userName.ToLower();
        RobozzleModel.RobozzleEntities entities = new RobozzleModel.RobozzleEntities();

        RobozzleModel.User user = entities.User.Where(u => u.Username == userName).FirstOrDefault();         
        if (user==null)
        {
            Response.Write(string.Format("Unknown user name."));
        }
        else
        {
            string encodedUserName = Server.HtmlEncode(userName);

            LevelWithDifficulty[] designedLevels = entities.LevelWithDifficulty.Where(l => l.SubmittedBy == userName).ToArray();
            int[] solvedLevels = entities.Solution.Where(l => l.UserName == userName && l.LevelId >= 0).GroupBy(l => l.LevelId).Select(g => g.Key).ToArray();

            int userCount = entities.User.Count();
            long? rank = entities.Scoreboard.Where(scoreRow => scoreRow.UserName == userName).Select(scoreRow => scoreRow.Rank).FirstOrDefault();

            var minLengthsForLevel = entities
                .Solution.Where(s => s.UserName != null && s.Solution1 != null && s.LevelId >= 0)
                .GroupBy(sol => sol.LevelId)
                .Select(p => new { LevelId = p.Key, MinLength = p.Min(x => x.Solution1.Length) })
                .ToDictionary(p => p.LevelId, p => p.MinLength);
            
            var minLengthsForUser = entities.Solution.Where(s => s.UserName == userName && s.Solution1 != null && s.LevelId >= 0)
                .GroupBy(sol => new { PuzzleId = sol.LevelId }).Select(p => new { LevelId = p.Key.PuzzleId, MinLength = p.Min(x => x.Solution1.Length) })
                .ToDictionary(p => p.LevelId, p => p.MinLength);

            var commentCounts =
                entities.ForumComment
                   .GroupBy(comment => comment.ForumThread)
                   .Select(g => new { PuzzleId = g.Key.PuzzleId, Count = g.Count() })
                   .Where(p => p.PuzzleId != null)
                   .ToDictionary(p => p.PuzzleId);

            string gravatarUrl = "http://www.gravatar.com/avatar/" + Md5EncodeText(user.Email ?? string.Empty) + ".jpg?r=PG&d=http%3A%2F%2Frobozzle.com%2Fimages%2F1x1t.gif";
     %>
     <h1>RoboZZle stats for <% Response.Write(Server.HtmlEncode(user.Username)); %></h1>
     <div style="margin-left:auto; margin-right:auto; width:80px;"><img src="<%= gravatarUrl %>"/></div>     
     
     <% if (user.About != null && user.About != "")
        { %>
     <p style="margin-left:auto; margin-right:auto; max-width:600px"><%= user.About%> </p>
     <% } %>
     
     <% if (user.Website != null && user.Website != "")
        {%>
     <p>Website: <a href="<%=Server.UrlPathEncode(user.Website) %>"> <%=Server.HtmlEncode(user.Website)%></a></p>
     <% } %>
     
     <h2>Designed puzzles (<% Response.Write(designedLevels.Length); %>)</h2>
     <table>
     <thead>
        <tr><th>Id</th> <th>Title</th> <th>Author</th> <th>Difficulty</th> <th>Liked / Disliked</th> <th></th><th></th></tr>
     </thead>
     
     <%
         int row = 0;
         foreach (var level in designedLevels)
         {
             string rowClass = StatsCommon.RowClass(row++);
             string difficulty, likedness;
             StatsCommon.ConvertVotes(level, out likedness, out difficulty);
             int commentCount = commentCounts.ContainsKey(level.Id) ? commentCounts[level.Id].Count : 0;
        %>
            <tr>
                <td class="<%=rowClass%>" style="text-align: left"><%= level.Id %></td>
                <td class="<%=rowClass%>" style="text-align: left"><a href="puzzle.aspx?id=<%= level.Id %>"><%= Server.HtmlEncode(level.Title) %></a></td>
                <td class="<%=rowClass%>" style="text-align: left"><a href="user.aspx?name=<%= level.SubmittedBy %>"><%= level.SubmittedBy %></a></td>
                <td class="<%=rowClass%>"><%= difficulty %></td>
                <td class="<%=rowClass%>"><%= likedness %></td>                
                <td class="<%=rowClass%>"><a href="index.aspx?puzzle=<%= level.Id %>">Play</a></td>
                <td class="<%=rowClass%>"><a href="forums/thread.aspx?puzzle=<%= level.Id %>"><%= StatsCommon.Plural("comment", commentCount) %></a></td>
            </tr>
        <%     
         }
    %>
    </table>
    <p>&nbsp;</p>
    <h2>Solved puzzles (<% Response.Write(solvedLevels.Length); %>)</h2>   
     <p>Rank: <% Response.Write(rank + " / " + userCount);%> </p>    
    <table>
    <tr>
     <%
        int counter = 0;
        foreach (int levelId in solvedLevels)
        {
            if (counter > 0 && counter % 8 == 0) Response.Write("</tr><tr>");
            bool isMinLen = minLengthsForUser.ContainsKey(levelId) && minLengthsForUser.ContainsKey(levelId) && (minLengthsForLevel[levelId] == minLengthsForUser[levelId]);
            Response.Write(
                string.Format(
                    "<td width='40'><a href='puzzle.aspx?id={0}'>{1}{0}{2}</a></td>",
                    levelId,
                    isMinLen ? "<b>" : "",
                    isMinLen ? "</b>" : ""));
            counter++;
        }
    %>  
    </tr>
    </table>
    <p><small>If the user submitted a minimum-length solution for this puzzle, the puzzle is shown in bold.</small></p>
    <%
        Response.Write(StatsCommon.Footer());            
        }
    %>
</body>
</html>
